const app = getApp();
const utils = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //默认城市
    locationCity: app.globalData.defaultCity,  
    //是否隐藏‘全文’文字
    isHiddenMore: false,
    //是否显示更多
    isMore: false,
    //是否隐藏遮罩  
    isHiddenAdd:true,
    //评论输入框是否隐藏
    discussInputHidden:true,
    //评论输入获取焦点
    commentInputFocus:false,

    currCommentIndex:0,
    currCommentOwnerShopId:0,
    currCommentPostType:0,
    currCommentPostId:0,
    currCommentUserId:0,
    currCommentContent:'',

    dataList: [],//数据列表
    pageIndex: 1, //当前页码
    pageSize: 5, //每页数量
    total: 0,   //总记录条数   
    hasMore: false,  //是否还有更多数据
    isLoadingMore: false,  //是否正在加载更多数据
    tipText: "正在加载更多数据..",
    //禁止遮罩层下面的页面滚动
    forbidScroll:false
  },
  //跳转到城市列表页面
  goToCityList: function() {
    wx.navigateTo({
      url: "citylist/citylist"
    })
  },

  /**
   * 跳转发单店铺
   */
  bindTapShopLink:function(e){
    var shopId=e.currentTarget.dataset.shopid;    
    var url = '../carcircle/othermy/othermy?shopid=' + shopId;
    utils.safeOpenUrl(url);
  },

  //打电话
  bindTapPhone: function(e) {
    var that=this;
    utils.safeDoAction(function(){
      var index = e.currentTarget.dataset.index;
      var dataList = that.data.dataList;
      var oInfo = dataList[index];     
      utils.postData(
        utils.apiUrl +"/message/AddPhoneCall",
        {
          OwnerShopID: oInfo.ShopID,
          PostType: oInfo.PostType,
          PostId: oInfo.PostID,
          CommentUserID: utils.getUserId(),
          FromType:1
        },
        function(){
          wx.makePhoneCall({
            phoneNumber: oInfo.Mobile
          });
        }
      );
    });    
  },
  /**   
    * 预览图片  
    */
  previewImage: function (e) {
    var that = this
    var rowindex = e.target.dataset.rowindex;
    var imgindex = e.target.dataset.imgindex
    var contentlistTem = that.data.dataList[rowindex].PostImgList;
    wx.previewImage({
      current: contentlistTem[imgindex], // 当前显示图片的http链接  
      urls: contentlistTem // 需要预览的图片http链接列表  
    })
  },
  bindMainTap:function(){
    var that=this;
    var dataList=that.data.dataList;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    dataList.forEach((item,index)=>{
      animation.width(0).step();
      item.animationData = animation.export();
      item.showCommentBtn=false;
    });
   that.setData({
     dataList:dataList,
     discussInputHidden: true,   
     commentInputFocus: false
   });
  },
  /**
   * 点赞
   */
  catchTapBtnLike:function(e){    
      var that=this;
      var dataset = e.currentTarget.dataset;
      var index = dataset.index;
      var postType = dataset.posttype;
      var postId = dataset.postid;
      var ownerShopId = dataset.ownershopid;
      var commentUserId = utils.getUserId();
      var animation = wx.createAnimation({
        duration: 150,
        timingFunction: 'linear',
      });
      that.animation = animation;
      
      var data = {
        OwnerShopID: ownerShopId,
        PostType: postType,
        PostId: postId,
        CommentUserID: utils.getUserId()
      }
       utils.postData(
        utils.apiUrl + '/Message/AddLike',
        data,
        function (response) { 
          var dataList = that.data.dataList;
          if (response.status == 0) {
            var data = response.data;
            dataList[index].LikeUser.push(data);
          }
          dataList.forEach((item, index) => {
            animation.width(0).step();
            item.animationData = animation.export();
            item.showCommentBtn = false;
          });
          dataList[index].showCommentBtn = false;
          that.setData({
            dataList: dataList
          })
        },
        {},
        function () {
       
          that.setData({
            isHiddenDiscuss: true
          })
        }
      )
  },
  //评论点击切换
  catchCommentBtnTap: function(e) {
    var that = this;
    console.log('撒娇的话比较卡');
    utils.safeDoAction(function () {   
    var index=e.currentTarget.dataset.index;
    var dataList = that.data.dataList;
    var isShowHid = dataList[index].showCommentBtn;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    dataList.forEach((item, index) => {
      animation.width(0).step();
      item.animationData=animation.export();
      item.showCommentBtn = false;      
    });
    if (isShowHid == true)
    {      
      //dataList[index].showCommentBtn = false;      
      animation.width("300rpx").step();
      dataList[index].animationData = animation.export();
      dataList[index].showCommentBtn = false;
      that.setData({
        dataList: dataList,
        discussInputHidden: true
      });
     
      setTimeout(function () {
        animation.width("0rpx").step()
        dataList[index].animationData = animation.export();
        this.setData({
          dataList: dataList
        })
      }.bind(that), 50);    
    }else
    {
      dataList[index].showCommentBtn = true;
      animation.width(0).step();
      dataList[index].animationData = animation.export();
      that.setData({
        dataList: dataList,
        discussInputHidden: true
      });
      setTimeout(function () {
        animation.width("300rpx").step()
        dataList[index].animationData = animation.export();
        this.setData({
          dataList: dataList
        })
      }.bind(that), 50);
    }
  

    });
  },
    /**
   * 点评论显示输入框，隐藏评论按键
   */
  catchTapBtnComment: function (e) {
    var that = this;

      var index = e.currentTarget.dataset.index;
      var dataList = that.data.dataList;
      dataList[index].showCommentBtn = false;
      var animation = wx.createAnimation({
        duration: 150,
        timingFunction: 'linear',
      });
      animation.width(0).step();
      dataList[index].animationData=animation.export();
      that.setData({
        dataList: dataList,
        discussInputHidden: false,
        commentInputFocus: true,
        currCommentIndex: index,
        currCommentOwnerShopId: dataList[index].ShopID,
        currCommentPostType: dataList[index].PostType,
        currCommentPostId: dataList[index].PostID,
        currCommentUserId: utils.getUserId(),
      })
     
  },
  /**
   * 绑定评论内容
   */
  bindCommentInput:function(e){
      this.setData({
        currCommentContent:e.detail.value
      });
  },
  /**
   * 评论
   */
  bindTapSendComment:function(e){
    var that = this;
    if (that.data.currCommentContent == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }
    var data = {
      OwnerShopID: that.data.currCommentOwnerShopId,
      PostType: that.data.currCommentPostType,
      PostId: that.data.currCommentPostId,
      CommentUserID: that.data.currCommentUserId,
      CommentContent: that.data.currCommentContent
    }
    utils.postData(
      utils.apiUrl + '/Message/AddComment',
      data,
      function (response) {       
        var currCommentIndex=that.data.currCommentIndex;
        var dataList = that.data.dataList;        
        if (response.status == 0) {
          dataList[currCommentIndex].CommentInfo.push(response.data);
          that.setData({
            dataList: dataList,            
            isShowComment: true,
            discussInputHidden: true,
            currCommentContent:""            
          })
        }
        else {
          wx.showModal({
            title: '评论失败',
            content: response.message,
            showCancel: false,
          });
        }
      },
      {},
      function () {
        
      }
    )
  },
  bindIconAddTap:function(){
     this.setData({
       isHiddenAdd: !this.data.isHiddenAdd,
       forbidScroll: !this.data.forbidScroll
     })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {   
    var that = this;
    that.getData();   
  },
  /**
   * 遮罩
   */
  bindTapMask:function(){
    this.setData({
      isHiddenAdd:true,
      forbidScroll: false      
    });
  },
  /**
   * 跳转收单
   */
  bindReceivePost:function(){
    var that=this;
    that.setData({
      isHiddenAdd: true,
      forbidScroll: false
    });
    utils.safeOpenUrl("/pages/carcircle/addreceivepost/addreceivepost",function(){
    
    });
  },
  /**
   * 跳转发单
   */
  bindSendPost:function(){
    var that = this;
    that.setData({
      isHiddenAdd: true,
      forbidScroll: false
    });
    utils.safeOpenUrl("/pages/carcircle/addsendpost/addsendpost", function () {
   
    });
  },
  /**
   * 加载动态数据
   */
  getData: function (cb) {
    var that = this;
    utils.getData(
      utils.apiUrl + '/CarCircle/GetSearchHot',
      {        
        page: that.data.pageIndex,
        rows: that.data.pageSize
      },
      function(response){
        if (response.status == 0) {
          console.log(response.data);
          var data = response.data;
          var dataList = data.items;
          var total = data.total;
          var hasMore = false;
          if (dataList.length < total) {
            hasMore = true;
          }
          that.setData({
            dataList: dataList,
            total: total,
            hasMore: hasMore
          });         
        }
      },
      function(){},
      function(){
       
      }
    );
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      locationCity: app.globalData.defaultCity
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
   
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // Do something when pull down.
    var that = this;
    that.setData({
      pageIndex: 1
    });
    that.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // Do something when page reach bottom.
    var that = this;
    if (!that.data.hasMore) {
      that.setData({
        tipText: "没有更多数据了"
      });
      return;
    }
    else {
      that.setData({
        tipText: "正在加载更多数据.."
      });
    }
    if (that.data.isLoadingMore) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      isLoadingMore: true
    });
    utils.getData(
      utils.apiUrl + '/CarCircle/GetSearchHot',
      {
        page: that.data.pageIndex,
        rows: that.data.pageSize
      },
      function (response) {
        if (response.status == 0) {
          var tempList = that.data.dataList;
          var data = response.data;
          var result = data.items;
          var total = data.total;
          var listCombine = tempList.concat(result);
          var hasMore = false;
          if (listCombine.length < total) {
            hasMore = true;
          }
          that.setData({
            dataList: listCombine,
            isLoadingMore: false,
            hasMore: hasMore
          });
        }
      },
      function () { },
      function () {
        wx.hideLoading();
      }
    );

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      // 来自页面右上角转发按钮
      return {
        title: '快来上车务圈，更多人脉，更多订单！限时免费注册！',
        path: '/pages/index/index',
        imageUrl: '/imgs/share-banner.jpg'
      };
    }
  },
  /**
   * 新车上牌
   */
  bindNewCar:function(){
    app.globalData.bizTypeId = 5;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
  /**
   * 车辆过户
   */
  bindTransferCar: function () {
    app.globalData.bizTypeId = 1;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 年检验车
   */
  bindCheckCar: function () {
    app.globalData.bizTypeId = 3;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 代领检字
   */
  bindRepresentativeToCollect: function () {
    app.globalData.bizTypeId = 4;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
     /**
   * 驾驶证补换
   */
  bindDriverLicense: function () {
    app.globalData.bizTypeId = 13;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 行驶证补换
   */
  bindTravelLicense: function () {
    app.globalData.bizTypeId = 12;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 登记证补换
   */
  bindRegisterLicense: function () {
    app.globalData.bizTypeId = 14;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 车牌补换
   */
  bindLicensePlate: function () {
    app.globalData.bizTypeId = 15;
    wx.switchTab({
      url: '/pages/carcircle/carcircle'
    })
  },
    /**
   * 跳转查询 页面
   */
  bindSearch:function()
  {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  
})